package com.tool.cn.mybaties.optimal.core;

import java.util.Map.Entry;

import com.tool.cn.mybaties.plugin.ClassFormat;
import com.tool.cn.mybaties.plugin.MysqlToJava;

/**
 * 
 * @author wangzonghui
 * @date 2022年5月10日 上午10:07:33 
 * @Description 创建xml内容
 */
public class CreateXmlContent {
	
	private String classFormat=ClassFormat.class.getName();
	
	private String mysqlJavaType=MysqlToJava.class.getName();

	private StringBuilder bud;
	public String createContent(ObjectModel objectModel) {
		
		bud=new StringBuilder();
		bud.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
		.append("\n<!DOCTYPE generatorConfiguration PUBLIC \"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN\" \"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd\">")
		.append("\n<generatorConfiguration>").append("<classPathEntry location=\""+objectModel.mysqlConnector+"\" />")
		.append("\n\t<context id=\"DB2Tables\" targetRuntime=\"MyBatis3\">")
		//开启toString功能
		.append("\n\t\t<plugin type=\"org.mybatis.generator.plugins.ToStringPlugin\"></plugin>")
		//设置model类模型
		.append("\n\t\t<commentGenerator type=\""+classFormat+"\">").append("<property name=\"suppressAllComments\" value=\"false\"/>").append("<property name=\"suppressDate\" value=\"false\"/>")
		.append("\n\t\t<property name=\"addRemarkComments\" value=\"true\"/> </commentGenerator>")
		.append("\n\t\t<jdbcConnection driverClass=\""+objectModel.jdbcDriver+"\" connectionURL=\""+objectModel.jdbcUrl+"\" userId=\""+objectModel.jdbcUser+"\" password=\""+objectModel.jdbcPwd+"\"><property name=\"useInformationSchema\" value=\"true\"/></jdbcConnection>")
		//设置没mysql与java类映射
		.append("\n\t\t<javaTypeResolver  type=\""+mysqlJavaType+"\"><property name=\"forceBigDecimals\" value=\"false\" /></javaTypeResolver>")
		//model配置
		.append("\n\t\t<javaModelGenerator targetPackage=\""+objectModel.modelPath+"\" targetProject=\""+objectModel.project+"\"><property name=\"enableSubPackages\" value=\"true\" /><property name=\"trimStrings\" value=\"true\" /></javaModelGenerator>")
		//xml文件
		.append("\n\t\t<sqlMapGenerator  targetPackage=\""+objectModel.xmlPath+"\" targetProject=\""+objectModel.xmlRootPath+"\"><property name=\"enableSubPackages\" value=\"true\" /></sqlMapGenerator>")
		//mapper配置
		.append("\n\t\t<javaClientGenerator type=\"XMLMAPPER\" targetPackage=\""+objectModel.mapperPath+"\" targetProject=\""+objectModel.project+"\"><property name=\"enableSubPackages\" value=\"true\" /></javaClientGenerator>");
		
		for(Entry<String, String> map:objectModel.tableRelation.entrySet()) {
			bud.append("\n\t\t<table tableName=\""+map.getKey()+"\" domainObjectName=\""+map.getValue()+"\" enableCountByExample=\"false\" enableUpdateByExample=\"false\" enableDeleteByExample=\"false\" enableSelectByExample=\"false\" selectByExampleQueryId=\"false\" />");
		}
		bud.append("\n\t</context>\n</generatorConfiguration>");
		return bud.toString();
	}
}
